#!/usr/bin/python
# -*- coding: utf-8 -*-
# Description: terminator unit test
# Create: 2008-7-13
# Author: MK2[fengmk2@gmail.com]

import os
import md5
import unittest
import terminator
import urllib2

from const import FileType

class Terminator_Test(unittest.TestCase):
    notdowns = (
                  ('http://192.168.18.20/web/Photo/..%2fKSPhoto%2f%5c%5b20080402%5d%d6%e9%ba%a3%b4%ba%d3%ce%a3%ad%e6%c4%d4%b4%2fCIMG0325.JPG', 
                   './samples/pp.jpg', './samples/pp_d.jpg', FileType.JPG),
                )
    
    downloads = (
#                 ('http://www.zhaodll.com/dll/download.asp?softid=25188&downid=2&id=25219', 
#                   './samples/AVModule.rar', './samples/AVModule_d.rar', FileType.RAR),
#                   ('http://41.duote.com/kssy.exe', 
#                    './samples/kssy.exe', './samples/kssy_d.exe', FileType.PE),

                    ('http://lp.downcode.com/j_08/j_8149_ly.rar', #'http://lp.downcode.com//j_08/j_8149_ly.rar', 
                   './samples/j_8149_ly.rar', './samples/j_8149_ly_d.rar', FileType.RAR),
                    
                 )
    
    smalldowns = (
#                  ('http://www.xdowns.com/soft/xdowns.asp?softid=43654&downid=35&id=46257',
#                   './samples/msxml4r.rar', './samples/msxml4r_d.rar', FileType.RAR),
#                  ('http://www.xdowns.com/soft/xdowns.asp?softid=40970&downid=10&id=43479',
#                   './samples/cmdialog.rar', './samples/cmdialog_d.rar', FileType.RAR),
#                  ('http://www.xdowns.com/soft/xdowns.asp?softid=42910&downid=57&id=45471',
#                   './samples/MSCOMCHS.rar', './samples/MSCOMCHS_d.rar', FileType.RAR),
                  )
    
    failurls = (
                 'http://www.zhaodll.com/dll/dowwnload.asp?softid=25188&downid=2&id=25219',
                 'http://www.dawfaw2132.com/adawd.exe',
                 'http://www.baidu.com/adawd.exe',
#                 'http://www.xdowns.com/soft/xdowns.asp?softid=43654&downid=35&id=46257', #防迅雷
                 'http://www.xdowns.com/soft/xdowns.asp?softid=40970&downid=10&id=43479', #防盗链
                 )
    
    ftpurls = (
               'fTp://127.0.0.1/28181658462.mp3',
               )
    
    ftperrorurls = ('ftp://127.0.0.1/2818165846222.mp3',
                    )
    
    def test_downloads(self):
        for url, lp, dp, wanttype in self.downloads:
            term = terminator.Terminator(url)
            print url
            term.terminate(dp)
            ff = open(lp, 'rb')
            md5_1 = md5.new(ff.read()).hexdigest()
            ff = open(lp, 'rb')
            a_md5 = md5.new(ff.read(4096)).hexdigest()
            ff.seek(term.b_offset, 0)
            b_md5 = md5.new(ff.read(2048)).hexdigest()
            ff.seek(-4096, 2)
            c_md5 = md5.new(ff.read(4096)).hexdigest()
            slice_md5 = md5.new('%s%s%s%d' \
                    % (a_md5, b_md5, c_md5, os.path.getsize(lp))).hexdigest()
            ff = open(dp, 'rb')
            md5_2 = md5.new(ff.read()).hexdigest()
            self.assertEqual(md5_1, md5_2)
            self.assertEqual(a_md5, term.a_md5)
            self.assertEqual(b_md5, term.b_md5)
            self.assertEqual(c_md5, term.c_md5)
            self.assertEqual(slice_md5, term.slice_md5)
            
    def test_notdowns(self):
        for url, lp, dp, wanttype in self.notdowns:
            term = terminator.Terminator(url)
            term.terminate(dp)
            self.assertTrue(not os.path.exists(dp))
            ff = open(lp, 'rb')
            a_md5 = md5.new(ff.read(4096)).hexdigest()
            ff.seek(term.b_offset, 0)
            b_md5 = md5.new(ff.read(2048)).hexdigest()
            ff.seek(-4096, 2)
            c_md5 = md5.new(ff.read(4096)).hexdigest()
            slice_md5 = md5.new('%s%s%s%d' \
                    % (a_md5, b_md5, c_md5, os.path.getsize(lp))).hexdigest()
            self.assertEqual(a_md5, term.a_md5)
            self.assertEqual(b_md5, term.b_md5)
            self.assertEqual(c_md5, term.c_md5)
            self.assertEqual(slice_md5, term.slice_md5)
    
    def test_smalldowns(self):
        for url, lp, dp, wanttype in self.smalldowns:
            print 'small', url
            term = terminator.Terminator(url)
            term.terminate(dp)
            ff = open(lp, 'rb')
            md5_1 = md5.new(ff.read()).hexdigest()
            ff = open(dp, 'rb')
            md5_2 = md5.new(ff.read()).hexdigest()
            self.assertEqual(md5_1, md5_2)
            self.assertTrue(term.smallfile, url)
#            
    def test_urlerror(self):
        for url in self.failurls:
            term = terminator.Terminator(url)
            self.assertRaises(urllib2.URLError, term.terminate, './test.exe')
    
    def test_ftperrorurls(self):
        
        for url in self.ftperrorurls:
            term = terminator.Terminator(url)
            self.assertRaises(IOError, term.terminate, './test.exe')
            
    def test_ftpurls(self):
        for url in self.ftpurls:
            term = terminator.Terminator(url)
            term.terminate('./testftp.exe')

if __name__ == '__main__':
    unittest.main()